<?php
/**
 * 时间段模型
 * @author jianhui
 *
 */
class timeUnitModel extends MY_Model{
    function timeUnitModel(){
        parent::MY_Model();
        $this->table = 'TimeUnit';
        $this->load->library('Cache');
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        foreach ($list as $k => $v){
            $v['singleTimeSetting'] = $v['singleTimeSetting'] ? unserialize($v['singleTimeSetting']) : array();
            $v['cityTimeSetting'] = $v['cityTimeSetting'] ? unserialize($v['cityTimeSetting']) : array();
            $v['xunTimeSetting'] = $v['xunTimeSetting'] ? unserialize($v['xunTimeSetting']) : array();
            $v['carTimeSetting'] = $v['carTimeSetting'] ? unserialize($v['carTimeSetting']) : array();
            $v['spreadTimeSetting'] = $v['spreadTimeSetting'] ? unserialize($v['spreadTimeSetting']) : array();
            $v['specialTimeSetting'] = $v['specialTimeSetting'] ? unserialize($v['specialTimeSetting']) : array();
            $v['specityTimeSetting'] = $v['specityTimeSetting'] ? unserialize($v['specityTimeSetting']) : array();
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 缓存时间配置
     */
    function createTimeUnitCache(){
        $yearList = array();//年份列表
        $timeUnitList = array();//时段列表
        $timeUnitDataList = array();//按年排列时段列表
        $findList = $this->getNewList(array('id > '=>0, 'startTime > ' => '2015-06-30 23:59:59'), null, null, '', 'year asc,orderNum asc');
        foreach ($findList as $v){
            //年份列表
            isset($yearList[(int)$v['year']]) || $yearList[(int)$v['year']] = (int)$v['year'].'年';
            //时段列表
            isset($timeUnitList[(int)$v['year']]) || $timeUnitList[(int)$v['year']] = array();
            $timeUnitList[(int)$v['year']][(int)$v['id']] = $v['name'];
            //时段数据列表
            isset($timeUnitDataList[(int)$v['year']]) || $timeUnitDataList[(int)$v['year']] = array();
            $v['startTime'] = strtotime($v['startTime']);
            $v['endTime'] = strtotime($v['endTime']);
            //单店时间转换为时间戳
            $singleTimeSetting = $v['singleTimeSetting'];
            foreach($singleTimeSetting as $tk => $tv){
                $singleTimeSetting[$tk] = strtotime($tv);
            }
            $v['singleTimeSetting'] = $singleTimeSetting;
            //城市提升转换为时间戳
            $cityTimeSetting = $v['cityTimeSetting'];
            foreach($cityTimeSetting as $tk => $tv){
                $cityTimeSetting[$tk] = strtotime($tv);
            }
            $v['cityTimeSetting'] = $cityTimeSetting;
            //巡展基金转换为时间戳
            $xunTimeSetting = $v['xunTimeSetting'];
            foreach ($xunTimeSetting as $tk => $tv){
                $xunTimeSetting[$tk] = strtotime($tv);
            }
            $v['xunTimeSetting'] = $xunTimeSetting;
            //车展基金转换为时间戳
            $carTimeSetting = $v['carTimeSetting'];
            foreach ($carTimeSetting as $tk => $tv){
                $carTimeSetting[$tk] = strtotime($tv);
            }
            $v['carTimeSetting'] = $carTimeSetting;
            //固定传播基金转换为时间戳
            $spreadTimeSetting = $v['spreadTimeSetting'];
            foreach ($spreadTimeSetting as $tk => $tv){
                $spreadTimeSetting[$tk] = strtotime($tv);
            }
            $v['spreadTimeSetting'] = $spreadTimeSetting;
            //专项单店基金转换为时间戳
            $specialTimeSetting = $v['specialTimeSetting'];
            foreach ($specialTimeSetting as $tk => $tv){
                $specialTimeSetting[$tk] = strtotime($tv);
            }
            $v['specialTimeSetting'] = $specialTimeSetting;
            //专项城市基金转换为时间戳
            $specityTimeSetting = $v['specityTimeSetting'];
            foreach ($specityTimeSetting as $tk => $tv){
                $specityTimeSetting[$tk] = strtotime($tv);
            }
            $v['specityTimeSetting'] = $specityTimeSetting;
            
            $timeUnitDataList[(int)$v['year']][(int)$v['id']] = $v;
        }
        $this->cache->set('yearList', $yearList);//缓存年份列表
        $this->cache->set('timeUnitList', $timeUnitList);//缓存时段列表
        $this->cache->set('timeUnitDataList', $timeUnitDataList);//缓存时段数据
    }
    /**
     * 获取时段相关数据
     * @param string $type
     * yearList：年份列表
     * timeUnitList：时段列表
     * timeUnitDataList：时段数据
     * @return array
     */
    function getCacheData($type){
        $this->cache->expire = 86400;//设置过期时间为一天
        $data = $this->cache->get($type);
        if (!$data){
            //创建缓存
            $this->createTimeUnitCache();
            $data = $this->cache->get($type);
        }
        return $data;
    }
    /**
     * 获取当前时段ID
     * @return Ambigous <>
     */
    function getNowTimeUnit(){
        $nowTime = strtotime("2016-02-29");
        //$nowTime = time();
        return $this->getTimeUnit($nowTime);
    }
    /**
     * 获取指定时间戳的时段ID
     * @param number $timeStamp
     * @return Ambigous <>
     */
    function getTimeUnit($timeStamp = 0){
        $year = date('Y', $timeStamp);
        if ($year != $this->CI->config->item('nowYear')) {
            $year = $this->CI->config->item('nowYear');
            $timeStamp = mktime(0, 0, 0, 12, 31, $year);
        }
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        $list = isset($timeUnitDataList[$year]) ? $timeUnitDataList[$year] : array();
        if ($list){
            foreach($list as $k => $v){
                if ($timeStamp > $v['startTime'] && $timeStamp < $v['endTime']){
                    return $v['id'];
                }
            }
        }
        return 0;
    }
    /**
     * 获取时段列表
     * @param number $year
     * @return Ambigous <multitype:, array>
     */
    function getTimeUnitList($year = 2016){
        $timeUnitList = $this->getCacheData('timeUnitList');
        return isset($timeUnitList[$year]) ? $timeUnitList[$year] : array();
    }
    /**
     * 获取已结束时段列表
     * @param number $year
     * @return array
     */
    function getFinishTimeUnitList($year = 2016){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        $timeUnitList = isset($timeUnitList[$year]) ? $timeUnitList[$year] : array();
        $newTimeUnitList = array();
        foreach ($timeUnitList as $k => $v){
            if ($v['endTime'] < time()){
                $newTimeUnitList[(int)$v['id']] = $v['name'];
            }
        }
        return $newTimeUnitList;
    }
    /**
     * 获取指定时段开始时间戳
     * @param int $timeUnit 时段ID
     * @param int $year 年
     */
    function getTimeUnitStartTime($timeUnit, $year){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitList[$year][$timeUnit]) ? $timeUnitList[$year][$timeUnit]['startTime'] : time();
    }
    /**
     * 获取指定时段开始日期
     * @param int $timeUnit
     * @param int $year
     * @return string
     */
    function getTimeUnitStartDate($timeUnit, $year){
        $startTime = $this->getTimeUnitStartTime($timeUnit, $year);
        return date("Y-m-d", $startTime);
    }
    /**
     * 获取指定时段结束时间戳
     * @param int $timeUnit 时段ID
     * @param int $year 年
     * @return int
     */
    function getTimeUnitEndTime($timeUnit, $year){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitList[$year][$timeUnit]) ? $timeUnitList[$year][$timeUnit]['endTime'] : time();
    }
    /**
     * 获取指定时段结束日期
     * @param int $timeUnit 时段ID
     * @param int $year 年
     * @return string
     */
    function getTimeUnitEndDate($timeUnit, $year){
        $endTime = $this->getTimeUnitEndTime($timeUnit, $year);
        return date("Y-m-d", $endTime);
    }
    /**
     * 获取指定时段的上个时段ID
     * @param int $year
     * @param int $timeUnit
     * @return number
     */
    function getPreTimeUnit($year, $timeUnit){
        $timeUnitList = $this->getCacheData('timeUnitList');
        if (isset($timeUnitList[$year])){
            $timeUnitIds = array_keys($timeUnitList[$year]);
            if (in_array($timeUnit, $timeUnitIds)){
                $key = array_search($timeUnit, $timeUnitIds);
                return isset($timeUnitIds[$key-1]) ? $timeUnitIds[$key-1] : 0;
            }
        }
        return 0;
    }
    /**
     * 获取指定时段的下个时段ID
     * @param int $year
     * @param int $timeUnit
     * @return number
     */
    function getNextTimeUnit($year, $timeUnit){
        $timeUnitList = $this->getCacheData('timeUnitList');
        if (isset($timeUnitList[$year])){
            $timeUnitIds = array_keys($timeUnitList[$year]);
            if (in_array($timeUnit, $timeUnitIds)){
                $key = array_search($timeUnit, $timeUnitIds);
                return isset($timeUnitIds[$key+1]) ? $timeUnitIds[$key+1] : 0;
            }
        }
        return 0;
    }
    /**
     * 获取开始和结束时间的时段列表
     * @param number $startTime
     * @param number $endTime
     */
    function getStartEntTimeUnitList($startTime, $endTime){
        //保证结束时间大于开始时间
        if ($startTime > $endTime){
            list($startTime, $endTime) =  array($endTime, $startTime);
        }
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        //所有时段列表
        $allTimeUnitList = array();
        foreach ($timeUnitDataList as $year => $timeUnitList){
            foreach ($timeUnitList as $timeK => $timeV){
                $allTimeUnitList[] = $timeV;
            }
        }
        //开始和结束范围内的时段列表
        $validTimeUnitList = array();
        foreach($allTimeUnitList as $v){
            if (($startTime <= $v['startTime'] && $v['startTime'] <= $endTime) || ($startTime <= $v['endTime'] && $v['endTime'] <= $endTime)){
                $data = array();
                $data['id'] = (int)$v['id'];
                $data['year'] = $v['year'];
                $data['name'] = $v['name'];
                $data['startTime'] = $v['startTime'] < $startTime ? $startTime : $v['startTime'];
                $data['endTime'] = $v['endTime'] < $endTime ? $v['endTime'] : $endTime;
                $data['months'] = $this->getStartEndMonths($data['startTime'], $data['endTime']);
                $validTimeUnitList[] = $data;
            }
        }
        return $validTimeUnitList;
    }
    /**
     * 计算开始和结束时间的所跨月份数
     * @param number $startTime
     * @param number $endTime
     */
    function getStartEndMonths($startTime, $endTime){
        //保证结束时间大于开始时间
        if ($startTime > $endTime){
            list($startTime, $endTime) =  array($endTime, $startTime);
        }
        return (date("Y", $endTime) - date("Y", $startTime)) * 12 + (date("m", $endTime) - date("m", $startTime)) + 1;
    }
    /**
     * 获取已经开始的时段集
     */
    function getStartedTimeUnitIds(){
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        //当前时间
        $nowTime = getNowTime();
        //所有时段列表
        $timeUnitIds = array();
        foreach ($timeUnitDataList as $year => $timeUnitList){
            foreach ($timeUnitList as $timeK => $timeV){
                if ($timeV['startTime'] <= $nowTime){
                    $timeUnitIds[] = (int)$timeV['id'];
                }
            }
        }
        return $timeUnitIds;
    }
    /**
     * 获取固定传播积极双月检核发布时间段集
     * @return [type] [description]
     */
    function getSpreadStartedTimeUnitIds(){
        //所有时段列表
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        $timeUnitIds = array();
        //当前时间
        $nowTime = getNowTime();
        foreach ($timeUnitDataList as $year => $timeUnitList){
            foreach ($timeUnitList as $timeK => $timeV){
                if ($timeV['spreadTimeSetting']['settleApplyStart'] <= $nowTime && $timeV['spreadTimeSetting']['settleApplyEnd'] >= $nowTime){
                    $timeUnitIds[] = (int)$timeV['id'];
                }
            }
        }
        return $timeUnitIds;
    }
    /**
     * 获取固定传播基金时段列表
     * @return unknown
     */
    function getSpreadDbmonthTimeUnitList(){
        //所有时段列表
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        $list = array();
        //合同开始结束时间
        $spreadContractStart = $this->config->item('spreadContractStart');
        $spreadContractEnd = $this->config->item('spreadContractEnd');
        //开始结束时间范围时间戳
        $startTime = strtotime($spreadContractStart.' 00:00:00');
        $endTime = strtotime($spreadContractEnd.' 23:59:59');
        //当前时间
        foreach ($timeUnitDataList as $year => $timeUnitList){
            foreach ($timeUnitList as $timeK => $timeV){
                if ($timeV['startTime'] >= $startTime && $timeV['endTime'] <= $endTime){
                    $list[(int) $timeV['id']] = array(
                        'id' => $timeV['id'],
                        'year' => $timeV['year'],
                        'name' => $timeV['name'],
                        'startTime' => $timeV['startTime'],
                        'endTime' => $timeV['endTime']
                    );
                }
            }
        }
        return $list;
    }
}